﻿using System;
using System.Linq;
using System.Reflection;

using RenewalProjects.Libraries.Wcf.EasyProxy.ChannelAllocators;
using RenewalProjects.Libraries.Wcf.EasyProxy.ChannelHolders;
using Rhino.Mocks;

namespace RenewalProjects.Libraries.Wcf.EasyProxy.Tests.TestBuilders
{
	public class ChannelAllocatorBuilder<TContract>
	{
		private readonly ChannelAllocatorBase<TContract> _channelAllocator;
		private readonly ChannelHolderBuilder<TContract> _channelHolderBuilder;
		
		public ChannelAllocatorBuilder(MockRepository mock, TContract testChannel)
		{
			ChannelFactoryBuilder<TContract> channelFactoryBuilder = new ChannelFactoryBuilder<TContract>(mock);
			_channelAllocator =
					mock.StrictMock<ChannelAllocatorBase<TContract>>(
							channelFactoryBuilder.GetFactory(),
							channelFactoryBuilder.GetServiceEndpoint());
			_channelHolderBuilder = new ChannelHolderBuilder<TContract>(mock, testChannel);
		}
		
		public ChannelAllocatorBuilder<TContract> ExpectAvailableChannelRetrieval()
		{
			_channelAllocator.Expect(x => x.GetAvailableChannel()).Return(_channelHolderBuilder.Build());
			return this;
		}
		
		public ChannelAllocatorBuilder<TContract> ChannelHolder(Action<ChannelHolderBuilder<TContract>> func)
		{
			func(_channelHolderBuilder);
			return this;
		}
		
		public ChannelAllocatorBase<TContract> Build()
		{
			return _channelAllocator;
		}
	}
}
